package com.eshidai.simple_springboot.common;

import java.util.HashMap;
import java.util.Map;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import com.eshidai.simple_springboot.bean.Email;


@Component
public class EmailMsgSend {
	@Autowired
	private JavaMailSender mailSender;
	
	public void sendSimpleMail(Email email) {
		SimpleMailMessage smm = new SimpleMailMessage();
		smm.setFrom(email.getSender());
		smm.setTo(email.getReceiver());
		smm.setSubject(email.getTopic());
		smm.setText(email.getContent());
		
		mailSender.send(smm);
	}
	
	public void sendTemplateMail(Email email, String...params) throws MessagingException {
		MimeMessage mimeMessage = mailSender.createMimeMessage();
		MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
		helper.setFrom(email.getSender());
		helper.setTo(email.getReceiver());
		helper.setSubject(email.getTopic());
		
		Map<String, Object> model = new HashMap<>();
		model.put("username", params[0]);
//		String text = VelocityEngineUtils.mergeTemplateIntoString(
//				velocityEngine, "template.vm", "UTF-8", model);
//		helper.setText(text, true);
		mailSender.send(mimeMessage);
	}
}
